package com.bzxy.monitor.until;

public class Constants
{
	public static final String ERR_ACCOUNT_NOT_EXSIT= "帐号不存在";
	public static final String ERR_ACCOUNT_HAS_EXSIT= "帐号已存在";
	public static final String ERR_PASSWORD			= "密码错误";
	public static final String ERR_UNKOWN			= "未知错误";
	public static final String SUC_VALIDE_PASS		= "验证成功";

	public static final String SUC_NEWUSER			= "新建帐号成功";
	public static final String ERR_NEWUSER			= "新建帐号失败";
	public static final String SUC_DELETEUSER		= "删除帐号成功";
	public static final String ERR_DELETEUSER		= "删除帐号失败";
	
	// database table name
	public static final String DB_TABLE_USER		= "_tUser";
	public static final String DB_TABLE_DEVICE		= "_tDevice";
	public static final String DB_TABLE_STATE_AIR_CONDITION = "_tProperty_AirConditioning";
	public static final String SUC_DEVICE_ADD		= "添加设备成功";
	public static final String ERR_DEVICE_ADD		= "添加设备失败";
	public static final String SUC_DEVICE_DEL		= "删除设备成功";
	public static final String ERR_DEVICE_DEL		= "删除设备失败";

	
	
	/*
	public static ShiroUser getCurrendUser(){
		Subject subject=SecurityUtils.getSubject();
		return (ShiroUser)subject.getSession().getAttribute(SHIRO_USER);
	}  
	
	/**  
	* 鍑芥暟鍔熻兘璇存槑 TODO:楂樼骇鏌ヨhql鏉′欢鎷兼帴
	* Administrator淇敼鑰呭悕瀛�
	* 2013-5-30淇敼鏃ユ湡
	* 淇敼鍐呭
	* @Title: getSearchConditionsHQL 
	* @Description: 
	* @param @param asName
	* @param @param searchColumnNames
	* @param @param searchAnds
	* @param @param searchConditions
	* @param @param searchVals
	* @param @return    璁惧畾鏂囦欢 
	* @return String    杩斿洖绫诲瀷 
	* @throws 
	*/
	/*
	public static String getGradeSearchConditionsHQL(String asName,PageUtil pageUtil)
	{
		String searchAnds = pageUtil.getSearchAnds();
		String searchColumnNames=pageUtil.getSearchColumnNames();
		String searchConditions=pageUtil.getSearchConditions();
		String searchVals=pageUtil.getSearchVals();
		if(null!=searchColumnNames && searchColumnNames.trim().length()>0){
			StringBuffer sb=new StringBuffer();
			String[] searchColumnNameArray=searchColumnNames.split("\\,");
			String[] searchAndsArray=searchAnds.split("\\,");
			String[] searchConditionsArray=searchConditions.split("\\,");
			String[] searchValsArray=searchVals.split("\\,");
			for (int i = 0; i < searchColumnNameArray.length; i++) {
				if (searchColumnNameArray[i].trim().length() > 0 && searchConditionsArray[i].trim().length()>0) {
					//if (i == 0) {
					//	sb.append(asName+"."+searchColumnNameArray[i].trim() + " " + searchConditionsArray[i].trim() + " " + searchValsArray[i].trim());
					//} else {
					//}
					String temp=searchValsArray[i].trim().replaceAll("\\'", "");
					if (HQL_LIKE.equals(searchConditionsArray[i].trim()))
					{
						sb.append(" " + searchAndsArray[i].trim() + " " + asName+IS_DOT+searchColumnNameArray[i].trim() + " " + searchConditionsArray[i].trim() + " " +"'%"+ temp+"%'");

					}else {
						sb.append(" " + searchAndsArray[i].trim() + " " + asName+IS_DOT+searchColumnNameArray[i].trim() + " " + searchConditionsArray[i].trim() + " " +"'"+ temp+"'");
					}
				}
			}
			if(sb.length()>0){
				return sb.toString();
			}
		}
		return NULL_STRING;
	}
	

	public static String getSearchConditionsHQL(String asName ,Map<String, Object> param){
		StringBuffer sb=new StringBuffer();
		if (param != null && !param.isEmpty()) {
			for (String name : param.keySet())
			{
				sb.append(" and "+asName+Constants.IS_DOT+name+" like :"+name+"");
			}
		}
		return sb.toString();
	}
	

	public static <T> void getLogs(Session session,T o,String eventName,String er,String name)
	{
		if (!LOGS_TB_NAME.equals(o.getClass().getSimpleName()))
		{
			String ip = getIpAddr();
			String mac=getMacAddr();
			String[] sdf = getFiledName(o);
			String id = getFieldValueByName(sdf[1], o).toString();
			Log l=new Log();
			l.setUserId(Constants.getCurrendUser().getUserId());
			l.setName(Constants.getCurrendUser().getAccount());
			l.setLogDate(new Date());
			l.setEventName(eventName+o.getClass().getSimpleName());
			l.setEventRecord(er+o.getClass().getName());
			l.setObjectId(id);
			l.setType(2);
			l.setIp(ip);
			l.setMac(mac);
			session.save(l);
		}
	}

	public static String[] getFiledName(Object o )
	{
		try
		{
			Field[] fields = o.getClass().getDeclaredFields();
			String[] fieldNames = new String[fields.length];
			for (int i = 0; i < fields.length; i++)
			{
				fieldNames[i] = fields[i].getName();
			}
			return fieldNames;
		} catch (SecurityException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public static Object getFieldValueByName(String fieldName, Object o)    
	{       
	   try    
	   {       
	       String firstLetter = fieldName.substring(0, 1).toUpperCase();       
	       String getter = "get" + firstLetter + fieldName.substring(1);       
	       Method method = o.getClass().getMethod(getter, new Class[] {});       
	       Object value = method.invoke(o, new Object[] {});       
	       return value;       
	   } catch (Exception e)    
	   {       
	       System.out.println("[Constants::getFiledName]Exception");       
	       return "";       
	   }       
	}  
	
	  public static HashMap<String, Method> ConverBean(Class<?> drbean) {  
	        Class<?> stopClass = null;  
	        // 瀛樻斁class淇℃伅  
	        BeanInfo drbeaninfo = null;  
	        // 瀛樻斁灞炴�淇℃伅  
	        PropertyDescriptor[] props;  
	        HashMap<String, Method> map = new HashMap<String, Method>();  
	        try {  
	            // 鑾峰彇class涓緱灞炴�鏂规硶淇℃伅  
	            drbeaninfo = Introspector.getBeanInfo(drbean, stopClass);  
	            // 鎶奵lass涓睘鎬ф斁鍏ropertyDescriptor鏁扮粍涓� 
	            props = drbeaninfo.getPropertyDescriptors();  
	            for (int i = 0; i < props.length; i++) {  
	                // 鑾峰彇灞炴�鎵�搴旂殑set鏂规硶  
	                Method setMethod = props[i].getWriteMethod();  
	                // 鍒ゆ柇灞炴�鏄惁鏈塻et鏂规硶 濡傛灉鏈夋斁鍏ap<灞炴�鍚嶏紝set鏂规硶>涓� 
	                if (setMethod != null) {  
	                    String field = props[i].getName().toLowerCase();  
	                    map.put(field, setMethod);  
	                }  
	            }  
	        } catch (IntrospectionException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	        return map;  
	    }
	
	
	public static String getIpAddr() {
		   HttpServletRequest request=ServletActionContext.getRequest();
	       String ip = request.getHeader("x-forwarded-for");
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getHeader("Proxy-Client-IP");
	       }
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getHeader("WL-Proxy-Client-IP");
	       }
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getRemoteAddr();
	       }
	       return ip;
	   } 
	

	public static String  getMacAddr()
	{
		String smac = "";
		try
		{
			UdpGetClientMacAddr umac = new UdpGetClientMacAddr(getIpAddr());
			smac = umac.GetRemoteMacAddr();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return smac;
	}
	private static final int BUFFER_SIZE = 16 * 1024;
	
	 public static void copy(File src, String fullSavePath) {
	        InputStream in = null; 
	        OutputStream out = null; 
	        File newFile=new File(fullSavePath);
	        try { 
	            in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); 
	            out = new BufferedOutputStream(new FileOutputStream(newFile), 
	                    BUFFER_SIZE); 
	            byte[] buffer = new byte[BUFFER_SIZE]; 
	            int len = 0; 
	            while ((len = in.read(buffer)) > 0) { 
	                out.write(buffer, 0, len); 
	            } 
	            out.flush();
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } finally { 
	            if (null != in) { 
	                try { 
	                    in.close(); 
	                } catch (IOException e) { 
	                    e.printStackTrace(); 
	                } 
	            } 
	            if (null != out) { 
	                try { 
	                    out.close(); 
	                } catch (IOException e) { 
	                    e.printStackTrace(); 
	                } 
	            } 
	        } 
	    }
	 public static String BASE_PATH =System.getProperty("erp");
	 
	 /
	 public static String dbBackUp()
		{
			//鐢熸垚涓存椂澶囦唤鏂囦欢
			SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmss");
			String fineName="dbBackUp-"+sd.format(new Date());
			String sqlName=fineName+Constants.FILE_SUFFIX_SQL;
			String pathSql=BASE_PATH+"attachment"+File.separator+"dbBackUp";
			try {
				File filePathSql = new File(pathSql);
				if(!filePathSql.exists()){
					filePathSql.mkdir();
				}
				StringBuffer sbs = new StringBuffer();
				sbs.append("mysqldump ");
				sbs.append("-h 192.168.110.10 ");
				sbs.append("--user=root");
				sbs.append(" --password=fortune123");
				sbs.append(" --lock-all-tables=true ");
				sbs.append("--result-file="+pathSql+File.separator);
				sbs.append(sqlName+" ");
				sbs.append(" --default-character-set=utf8 ");
				sbs.append("ERP");
		        Runtime runtime = Runtime.getRuntime();
		        Process child = runtime.exec(sbs.toString());
		        //璇诲彇澶囦唤鏁版嵁骞剁敓鎴愪复鏃舵枃浠�
		        InputStream in = child.getInputStream();
		        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(pathSql), "utf8");
		        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf8"));
		        String line=reader.readLine();
		        while (line != null) {
		                writer.write(line+"\n");
		                line=reader.readLine();
		         }
		         writer.flush();
			} catch (Exception e) {
				
			}
			return sqlName;
		}
		*/
}
